package com.white.controller;

import com.white.bean.ThumbsUp;
import com.white.mapper.ThumbsUpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ThumbsUpController {
    /*图书类型*/
    public static final int ARTICLE_TYPE = 0;
    /*评论类型*/
    public static final int COMMENT_TYPE = 1;
    @Autowired
    ThumbsUpMapper thumbsUpMapper;

    @GetMapping("/isLikeArticle/{articleId}")
    @ResponseBody
    public boolean isLikeArticle(@CookieValue(UserController.LOGIN_USER_ID) Long userId,
                                 @PathVariable("articleId") Long articleId) {
        return thumbsUpMapper.isLikeArticle(userId, articleId).size() > 0;
    }


    @GetMapping("/unLikeArticle/{articleId}")
    @ResponseBody
    public void unLikeArticle(@CookieValue(UserController.LOGIN_USER_ID) Long userId,
                                  @PathVariable("articleId") Long articleId) {

        thumbsUpMapper.myDelete(userId, articleId, ARTICLE_TYPE);
    }


    @GetMapping("/likeArticle/{articleId}")
    @ResponseBody
    public void likeArticle(@CookieValue(UserController.LOGIN_USER_ID) Long userId,
                            @PathVariable("articleId") Long articleId) {
        thumbsUpMapper.save(new ThumbsUp(userId, articleId, ARTICLE_TYPE));
    }

    @GetMapping("/unLikeComment/{id}")
    @ResponseBody
    public void unLikeComment(@CookieValue(UserController.LOGIN_USER_ID) Long userId,
                                  @PathVariable("id") Long id) {

        thumbsUpMapper.myDelete(userId, id, COMMENT_TYPE);
    }


    @GetMapping("/likeComment/{id}")
    @ResponseBody
    public void likeComment(@CookieValue(UserController.LOGIN_USER_ID) Long userId,
                            @PathVariable("id") Long id) {
        thumbsUpMapper.save(new ThumbsUp(userId, id, COMMENT_TYPE));
    }

}
